import { intoRecurse, deepCopy } from '@u/common.js';

export default {
  async created() {
    this.createFn && this.createFn();

    if (this.pageName) {
      const pages = this.$store.getters.pages[this.pageName],
        page = pages && pages.page;

      this.index = page || this.index;
      await this.getTreeData(this.treeFnCallback);

      this.getPagingList();
      this.$store.commit('layout/removePages', this.pageName);
    }
  },
  data() {
    return {
      pageName: '',
      treeSearchKey: {},
      initActive: 0,
      treeId: 0,
      treeData: [],
      searchKey: {},
      index: 1,
      size: 10,
      totalSize: 0,
      pagingList: [],
      delMessage: {
        msg: '确定删除吗？',
        title: '删除确认',
        options: {}
      }
    };
  },
  methods: {
    async getTreeData(callback) {
      await this.treeFn({
        params: {
          ...this.treeSearchKey,
        }
      }).then(res => {
        if (res.code == 200) {
          const pages = this.$store.getters.pages[this.pageName],
            chains = pages && pages.chains,
            resData = callback && callback(res.data);
          this.treeData = resData || res.data;
          if (!this.treeId && this.treeData.length) {
            this.treeId = this.treeData[0].id;
          }
          if (chains) {
            this.treeId = chains[chains.length - 1];
            if (chains.length) {
              const list = deepCopy(this.treeData);
              intoRecurse(list, (item, options) => {
                if (item.id == chains[chains.length - 1]) {
                  this.handleChainsCallback && this.handleChainsCallback(item);
                }
                const i = chains.findIndex(r => r == item.id);
                if (i != -1) {
                  item.isOpen = true;
                }
                if (i == chains.length - 1) {
                  options.close = true;
                }
              }, { childrenName: 'subList' });
              this.treeData = list;
            }
          }
        }
      });
    },
    changeTreeItem(item) {
      this.treeId = item.id;
      this.treeItemCallback && this.treeItemCallback(item);
      this.getPagingList(1);
    },
    getPagingList(i) {
      this.index = i || this.index;
      this.listFn({
        params: {
          index: this.index,
          size: this.size,
          ...this.searchKey
        }
      }).then(res => {
        if (res.code == 200) {
          this.listFnCallback && this.listFnCallback(res.data.list);
          this.pagingList = res.data.list;
          this.totalSize = res.data.totalSize;
        }
      });
    },
    handlePagination({ page, limit }) {
      this.index = page;
      this.size = limit;
      this.getPagingList();
    },
    changeState(row) {
      this.stateFn(row.id, {
        data: {
          stateId: row.state.id,
        }
      }).then(res => {
        if (res.code != 200) {
          row.state.id = row.state.id == 1 ? 2 : 1;
        } else {
          this.stateCallback && this.stateCallback({ row });
        }
      });
    },
    del(row, i, callback, options = {}) {
      callback && callback(options);
      if (options.close) {
        return;
      }
      this.$confirm(this.delMessage.msg, this.delMessage.title, {
        type: 'warning',
        ...this.delMessage.options
      }).then(() => {
        this.delFn(row.id).then(async res => {
          if (res.code == 200) {
            await this.getTreeData(this.totalSize == 1 && this.treeFnCallback);
            this.deleteCallback && this.deleteCallback({ row, isHave: this.totalSize > 1 });
            if (this.pagingList.length == 1) {
              this.getPagingList(this.index - 1);
            } else {
              this.getPagingList();
            }
          }
        });
      }).catch(() => {});
    }
  },
  components: {
    Tree: () => import('@c/Tree'),
    Pagination: () => import('@c/Pagination.vue')
  }
};
